/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __MESH_H__
#define __MESH_H__

#include "object.h"
#include "triangle.h"
#include "quadrilateral.h"
#include "math/vector.h"
#include "math/ray.h"
#include "material/object_material.h"

#include <string>
#include <vector>

#include <boost/tuple/tuple.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>

using namespace boost::tuples;

namespace Sirat {

namespace Object {

class Mesh : public Object {
public:
	Mesh(Material::Object_Material* _object_material,const std::string& _filename,const unsigned short& _type);
	Mesh();
	Mesh(const Mesh& _mesh);
	~Mesh();

	std::string get_filename() const;
	virtual Math::Vector get_normal(const Math::Vector&) const;

	virtual tuple<double,Math::Vector,Object*> intersection(const Math::Ray&);
	
	void scale(const double);
	void translate(const Math::Vector&);

	Mesh& operator=(const Mesh& _mesh);

	virtual bool is_set() const { return true; }

	virtual const std::vector<Object*> get_object_list() const { return mesh; }
	Object* clone() { return new Mesh(*this); }
private:
	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);

	void load();
	void load_quadrilateral(const std::string&);
	void load_triangle(const std::string&);
	
	std::vector<Object*> mesh;
	std::string filename;
	unsigned short type;
	
	bool loaded;
};



template<class Archive> void Mesh::serialize(Archive& ar, const unsigned int /* file_version */){
	ar	& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Object)
		& BOOST_SERIALIZATION_NVP(type)	
		& BOOST_SERIALIZATION_NVP(filename)
		& BOOST_SERIALIZATION_NVP(mesh)
		& BOOST_SERIALIZATION_NVP(loaded);
}

}

}

#endif
